package com.changanw.middleware.dbrouter.config;

import com.changanw.middleware.dbrouter.DBRouterConfig;
import com.changanw.middleware.dbrouter.dynamic.DynamicDataSource;
import com.changanw.middleware.dbrouter.util.PropertyUtil;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author changanw  用于启动的时候加载配置信息，以及创建数据源
 * @description  配置加载和创建数据源
 * @date 2024/5/10 13:25
 */
@Configuration
public class DataSourceAutoConfig implements EnvironmentAware {
  private Map<String, Map<String, Object>> dataSourceMap = new HashMap<>();

  private int dbCount;  //分库数
  private int tbCount;  //分表数

  @Bean
  public DBRouterConfig dbRouterConfig() {
    return new DBRouterConfig(dbCount, tbCount);
  }
  @Bean
  public DataSource dataSource() {
    // 创建数据源
    Map<Object, Object> targetDataSources = new HashMap<>();
    for (String dbInfo : dataSourceMap.keySet()) {
      Map<String, Object> objMap = dataSourceMap.get(dbInfo);
      targetDataSources.put(dbInfo, new DriverManagerDataSource(objMap.get("url").toString(), objMap.get("username").toString(), objMap.get("password").toString()));
    }
    // 设置数据源
    DynamicDataSource dynamicDataSource = new DynamicDataSource();
    dynamicDataSource.setTargetDataSources(targetDataSources);
    return dynamicDataSource;
  }

  /**
   * setEnvironment，读取自定义配置，因为我们要设置的是一个在 yml 配置多组数据源
   * @param environment
   */
  @Override
  public void setEnvironment(Environment environment) {
    String prefix = "router.jdbc.datasource.";

    dbCount = Integer.valueOf(environment.getProperty(prefix + "dbCount"));
    tbCount = Integer.valueOf(environment.getProperty(prefix + "tbCount"));

    String dataSources = environment.getProperty(prefix + "list");
    for (String dbInfo : dataSources.split(",")) {
      Map<String, Object> dataSourceProps = PropertyUtil.handle(environment, prefix + dbInfo, Map.class);
      dataSourceMap.put(dbInfo, dataSourceProps);
    }
  }
}
